<template>
  <div>
    <div
      :style="verifyBtnStyleComputed"
      class="verifyButtonCommonClass"
      @click="btnClick"
      @mouseover="verifyButtonMouseOver($event)" @mouseout="verifyButtonMouseOut($event)"
    >{{buttonMsg}}
    </div>
  </div>
</template>

<script>
  import commonUtil from "../../util/common";

  export default {
    props: {
      btnStyle: {
        width: {type: String, default: '96px'},
        height: {type: String, default: '48px'}
      },
      btnMsg: {
        type: String,
        required: true,
        default: ''
      },
      phone: '',
      url: ''
    },
    data() {
      return {
        buttonMsg: '',
        btnWidth: '96px',
        btnHeight: '48px',
        canClick: true,
        timeLoop: 60
      }
    },
    mounted() {
      let {btnMsg} = this.$props
      this.buttonMsg = btnMsg
    },
    computed: {
      verifyBtnStyleComputed() {
        let {btnStyle} = this.$props
        if (btnStyle === undefined) {
          btnStyle = {}
        }
        if (!btnStyle.hasOwnProperty('height')) {
          btnStyle['height'] = this.btnHeight
        }
        if (!btnStyle.hasOwnProperty('width')) {
          btnStyle['width'] = this.btnWidth
        }
        btnStyle[`line-height`] = btnStyle.height

        return btnStyle
      }
    },
    methods: {
      btnClick() {
        let {phone} = this.$props
        this.$emit('checkPhone', this.name)
        if (phone === undefined || !commonUtil.checkPhone(phone)) {
          return
        }
        if (!this.canClick) return
        this.canClick = false
        // 发送验证码
        this.$axios({
          url: this.url,
          method: 'post',
          params: {
            "phone": phone
          }
        }).then(res => {
          if (res.state !== 0) {
            this.canClick = true
            alert(res.msg)
          } else {
            this.timeLoop = 60
            let lock = setInterval(() => {
              this.buttonMsg = this.timeLoop-- + 's'
              if (this.timeLoop < 0) {
                window.clearInterval(lock)
                this.timeLoop = 60
                this.canClick = true
                this.buttonMsg = '重新获取'
              }
            }, 1000)
          }
        })
      },
      verifyButtonMouseOver($event) {
        $event.currentTarget.className = 'verifyButtonCommonClass buttonCommonMouseOverClass'
      },
      verifyButtonMouseOut($event) {
        $event.currentTarget.className = 'verifyButtonCommonClass'
      }
    }
  }
</script>

<style>
  .verifyButtonCommonClass {
    margin-left: 14px;
    background: rgba(95, 167, 124, 1);
    border-radius: 8px;
    text-align: center;
    cursor: pointer;
    border-left: 14px;
    font-size: 14px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: rgba(220, 220, 220, 1);
  }
  .buttonCommonMouseOverClass {
    color: #111111;
    background: #30ff10;
  }
</style>
